package com.hopes.bizz.video.service.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopes.bizz.video.config.HikSyncConfig;
import com.hopes.bizz.video.service.entity.VideoRegionEntity;
import com.hopes.bizz.video.service.mapper.VideoRegionMapper;
import com.hopes.bizz.video.service.service.VideoRegionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 视频区域
 *
 * @author daijie
 * @date 2023-10-24 13:40:17
 */
@Slf4j
@Service
public class VideoRegionServiceImpl extends ServiceImpl<VideoRegionMapper, VideoRegionEntity> implements VideoRegionService {
    @Autowired
    private HikSyncConfig hikSyncConfig;

    @Override
    public Boolean sync() {
        // 删除所有区域
        this.remove(new LambdaQueryWrapper<>());

        // 查询海康区域
        VideoRegionEntity videoRegion = new VideoRegionEntity();
        videoRegion.setId(0L);
        this.syncSingleRegion(videoRegion, hikSyncConfig);
        return true;
    }

    @Override
    public List<Tree<Long>> tree(VideoRegionEntity videoRegion) {
        List<VideoRegionEntity> regionList = this.list(
                Wrappers.<VideoRegionEntity>lambdaQuery().like(StrUtil.isNotBlank(videoRegion.getName()), VideoRegionEntity::getName, videoRegion.getName()).orderByAsc(VideoRegionEntity::getSortNum));
        List<TreeNode<Long>> tree = regionList.stream().map(region -> {
            TreeNode<Long> treeNode = new TreeNode<>();
            treeNode.setId(region.getId());
            treeNode.setParentId(region.getParentId());
            treeNode.setName(region.getName());
            treeNode.setWeight(region.getSortNum());

            Map<String, Object> extra = new HashMap<>();
            extra.put("hikIndexCode", region.getHikIndexCode());
            extra.put("remark", region.getRemark());
            treeNode.setExtra(extra);
            return treeNode;
        }).collect(Collectors.toList());

        return TreeUtil.build(tree, 0L);
    }

    private void syncSingleRegion(VideoRegionEntity parentRegion, HikSyncConfig hikSyncConfig){
        JSONArray regionArray = hikSyncConfig.hikSyncRegion(parentRegion.getHikIndexCode());
        log.debug("视频区域同步结果：{}", regionArray);
        for(int i = 0; i < regionArray.size(); i++){
            JSONObject regionO = regionArray.getJSONObject(i);
            VideoRegionEntity videoRegion = new VideoRegionEntity();
            videoRegion.setName(regionO.getString("name"));
            videoRegion.setHikIndexCode(regionO.getString("indexCode"));
            videoRegion.setParentId(parentRegion.getId());
            this.save(videoRegion);
            this.syncSingleRegion(videoRegion, hikSyncConfig);
        }
    }
}
